package models.venue.room;

import jodd.bean.BeanCopy;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.enums.ChargeMode;
import models.venue.enums.OccupiedStatus;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 包房
 * Created by hanbin on 17/4/6.
 */
@Entity
@Table(name="room_infos")
public class Room  extends BaseModel {




    /**
     * 包房名称
     */
    @Column(name="room_name", length = 255)
    public String roomName;

    /**
     * 所属店面
     */
    @JoinColumn(name="merchant_id")
    @ManyToOne
    public Merchant merchant;

    /**
     * 描述
     */
    @Column(name="remark", length = 255)
    public String remark;

    /**
     * 交易量
     */
    @Column(name = "trading_volume")
    public Integer tradingVolume;

    /**
     * 每小时价格
     */
    @Column(name = "per_price")
    public BigDecimal perPrice;

    /**
     * 计费模式
     */
    @Enumerated(EnumType.STRING)
    @Column(name="charge_mode", length = 255)
    public ChargeMode chargeMode;

    /**
     * 图片
     */
    @Column(name = "room_image")
    public String roomImage;

    /**
     * 包房占用状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name="room_status", length = 255)
    public OccupiedStatus occupiedStatus;

    /**
     * 已预约的包房tooltip
     */
    public String toolTip;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Room> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.roomName like {roomName} ~/")
                .append("/~ and t.occupiedStatus = {occupiedStatus} ~/")
                .append("/~ and t.merchant.id = {merchantId} ~/");

        //if(conditionMap.get("payedOrder")!=null)xsqlBuilder.append(" and (t.status ="+OrderStatus.PAID+" or t.status="+OrderStatus.DISTRIBUTION+") ");

        /**
         *    Query query = t.id = {id};
         *    query.setPropery("id" , conditionMap.get("id"));
         */
        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Room> orderPage = new JPAExtPaginator<Room>("Room t", "t", Room.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }

    /**
     * Update
     * @param id
     * @param newObject
     */
    public static void update(Long id, Room newObject) {
        Room room = Room.findById(id);
        BeanCopy.beans(newObject, room).ignoreNulls(true).copy();
        room.save();
    }

    /**
     * 根据商户查找健身类型
     * @return
     */
    public static List<Room> findAllRoomByMerchant(long id){
        return find("deleted = ? and merchant.id = ? order by id",DeletedStatus.UN_DELETED, id).fetch();
    }


}
